package com.jkj.http.sys.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jkj.http.controller.BaseController;
import com.jkj.http.entity.LayPadding;
import com.jkj.http.entity.TreeSelect;
import com.jkj.http.interceptor.AuthorizeChecked;
import com.jkj.http.sys.entity.SysOrganize;
import com.jkj.http.sys.entity.SysRole;
import com.jkj.http.sys.service.SysRoleService;
import com.jkj.utils.JsonUtils;
import com.jkj.utils.OperatorProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;


@RestController
@RequestMapping(value = "/system/role")
public class RoleController extends BaseController {
    @Autowired
    private SysRoleService roleService;

    /**
     * 角色列表页
     *
     * @return
     */
    @AuthorizeChecked()
    @RequestMapping(value = "/index", method = {RequestMethod.GET})
    public ModelAndView index() {
        return new ModelAndView("system/role/index");
    }


    /**
     * 角色列表查询（分页）
     *
     * @param pageIndex
     * @param pageSize
     * @param keyWord
     * @return
     */
    @AuthorizeChecked()
    @RequestMapping(value = "/index", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
    public String index(int pageIndex, int pageSize, String keyWord) {
        int totalCount = roleService.getTotalCount(keyWord);
        PageHelper.startPage(pageIndex, pageSize);//这行是重点，表示从pageNum页开始，每页pageSize条数据
        List<SysRole> list = roleService.getList(keyWord);
        PageInfo<SysRole> pageInfo = new PageInfo<>(list);

        List<SysRole> pageData = pageInfo.getList();
        LayPadding<SysRole> result = new LayPadding<SysRole>();
        result.setResult(true);
        result.setMsg("success");
        result.setList(pageData);
        result.setCount(totalCount);
        return JsonUtils.toJSON(result);
    }

    /**
     * 角色新增修改页面
     *
     * @return
     */
    @AuthorizeChecked()
    @RequestMapping(value = "/form", method = {RequestMethod.GET})
    public ModelAndView form() {
        return new ModelAndView("system/role/form");
    }

    /**
     * 角色新增或者修改
     *
     * @param session
     * @param model
     * @return
     */
    @AuthorizeChecked()
    @RequestMapping(value = "/form", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
    public String form(HttpSession session, SysRole model) {
        if (StrUtil.isEmpty(model.getId())) {
            model.setCreateUser(OperatorProvider.getCurrent(session).getAccount());
            int row = roleService.insert(model);
            return row > 0 ? success() : error();
        } else {
            model.setModifyUser(OperatorProvider.getCurrent(session).getAccount());
            int row = roleService.update(model);
            return row > 0 ? success() : error();
        }
    }

    /**
     * 角色详情页面
     *
     * @return
     */
    @AuthorizeChecked()
    @RequestMapping(value = "/detail", method = {RequestMethod.GET})
    public ModelAndView detail() {
        return new ModelAndView("system/role/detail");
    }

    /**
     * 根据主键得到角色信息
     *
     * @param primaryKey
     * @return
     */
    @RequestMapping(value = "/getForm", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
    public String getForm(String primaryKey) {
        SysRole entity = roleService.get(primaryKey);
        entity.setIsEnabled(entity.getIsEnabled().equals("1") ? "true" : "false");
        entity.setAllowEdit(entity.getAllowEdit().equals("1") ? "true" : "false");
        return JsonUtils.toJSON(entity);
    }

    /**
     * 删除角色信息
     *
     * @param primaryKey
     * @return
     */
    @AuthorizeChecked()
    @RequestMapping(value = "/delete", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
    public String delete(String primaryKey) {
        int row = roleService.delete(primaryKey.split(","));
        return row > 0 ? success() : error();
    }

    /**
     * 得到角色列表
     *
     * @return
     */
    @RequestMapping(value = "/getListTreeSelect", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
    public String getListTreeSelect() {
        List<SysRole> listRole = roleService.getRoleList();
        List<TreeSelect> listTree = new ArrayList<TreeSelect>();
        for (SysRole item : listRole) {
            TreeSelect model = new TreeSelect();
            model.setId(item.getId());
            model.setText(item.getName());
            listTree.add(model);
        }
        return JsonUtils.toJSON(listTree);
    }
}
